<?php
class Pieces extends Controller {

	function Pieces()
	{
		parent::Controller();
		$this->load->helper('form');
		$this->load->model('Piece_model','Piece');
		$this->load->model('Rooms_model', 'Rooms');
		$this->load->model('Exposition_model', 'Exposition');
	}

	function index()
	{
		$this->load->view('layout/header');
		$this->load->view('layout/footer');
	}
	
	function view($id)
	{
		// TODO creer vue piece_view 
		$query = $this->Piece->get($id);
		$data['piece'] = $query;	
		$this->load->view('piece_view',$data);
		}
		
	function mobile_view($id)
	{ 
		$query = $this->Piece->get($id);
		$data['piece'] = $query;	
		$this->load->view('mobile_piece',$data);
	}
		
	// To view the qr code of the piece
	function qr($id){
		$data['id'] = $id;
		$query = $this->Piece->get($id);
		$data['piece'] = $query;	
		$this->load->view('qr_code',$data);
	}
	// To edit piece of id $id or add a new piece
	function edit($id = '')
	{
		if (!$this->tank_auth->is_logged_in()) {
			redirect('/auth/login/');
		}
		else{
			
			// Recuperer l'expo de l'utilisateur loggu�
			$user_id = $this->tank_auth->get_user_id();
			$expo_id = $this->Exposition->get_expo_from_user($user_id);
			// R�cuperer l'oeuvre que l'on veut �diter
			
			$query = $this->Piece->get($id);
			

			if($query->num_rows() > 0){
					
				// Si l'oeuvre existe d�j�
				// on enregistre dans la variable piece de data les donnes concernant l'oeuvre
				$row = $query->row();
				$data['piece'] = array('id'=>$id,'room_id'=>$row->room_id,'name'=>$row->name,'author'=>$row->author,'description'=>$row->description,'date_creation'=>$row->date_creation,'ressources'=>$row->ressources, 'photo'=>$row->photo);
				// Recuperer les infos sur la piece de l'oeuvre
				$query_room = $this->Rooms->get_room_from_piece($id);
				$room = $query_room->row()->id;
			}
			else{

				// Sinon, on passe des donn�es vides sur l'oeuvre
				$data['piece'] = array('id'=>-1,'room_id'=>'','name'=>'','author'=>'','description'=>'','date_creation'=>'','ressources'=>'', 'photo'=>'');
				$room = 0;
				$selected = $this->Rooms->get_room($expo_id,6,4)->row()->id;
			}
			

			/* Param�tre pour le select de la salle de l'oeuvre
			 * on r�cup�re toutes les salles de l'expo
			 * on les parcourt :
			 * si l'oeuvre est dans la salle on met l'info dans $selected
			 * si l'oeuvre n'y est pas on ne fait que mettre l'info dans $options
			 * $options et $selected seront pass�es � la view dans data et utilis�es pour cr�er une dropdown
			 */
			$rooms = $this->Rooms->get_rooms($expo_id);

			foreach($rooms->result() as $row){
				
				$options[$row->id] = $row->name;
				
				if ($row->id == $room){
					
					$selected = $row->id;
				}
			}
			
			$data['options'] = $options;
			$data['selected'] = $selected;

			$this->load->view('layout/header_admin');
			$this->load->view('edit_piece', $data);
			$this->load->view('layout/footer');

		}
	}

	function delete($id)
	{

		$this->db->where('id', $id);
		$this->db->delete('pieces');


		$user_id = $this->tank_auth->get_user_id();
		$expo_id = $this->Exposition->get_expo_from_user($user_id);

		redirect("expositions/owner_home/");

	}

	function agrandir($img){
		$data['img'] = $img;
		$this->load->view("image_view", $data);
	}
	function confirm_changes()
	{
		if ($_POST['id']!=(-1)){

			$this->db->where('id',$_POST['id']);
			$this->db->update('pieces', array('name'=>$_POST['name'], 'description'=>$_POST['description'], 'date_creation'=>$_POST['date_creation'], 'room_id'=>$_POST['room_id'], 'author'=>$_POST['author'], 'photo' =>$_POST['upload']));
		}
		else{
			$this->db->insert('pieces', array('name'=>$_POST['name'], 'room_id'=>$_POST['room_id'], 'description'=>$_POST['description'], 'date_creation'=>$_POST['date_creation'], 'room_id'=>$_POST['room_id'],'photo' =>$_POST['upload'], 'author'=>$_POST['author']));
		}
			
		redirect("expositions/owner_home/");
	}

}

